<?php namespace Phpcmf\Controllers\Admin;

class Module extends \Phpcmf\Common
{

    public function index() {

        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');
        if (!$module) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        $data = [];
        foreach ($module as $t) {
            // 商城模块不允许安装订单插件
            if ($t['dirname'] != 'store') {
                $m = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$t['dirname']);
                if ($m['field']['price']) {
                    $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$t['dirname'];
                    $is_field = 1;
                    foreach (['price', 'price_sku', 'price_quantity', 'price_sn'] as $fname) {
                        if (!\Phpcmf\Service::M()->db->fieldExists($fname, $table)) {
                            $is_field = 0;
                            break;
                        }
                    }
                } else {
                    $is_field = 0;
                }
                $data[] = [
                    'name' => $t['name'],
                    'dirname' => $t['dirname'],
                    'is_field' => $is_field,
                ];
            }
        }
        if (!$data) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '内容模块权限' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-gears'],
                    'help' => [636],
                ]
            ),
            'data' => $data,
        ]);
        \Phpcmf\Service::V()->display('module_qx.html');
    }


    public function edit() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_admin_msg(0, dr_lang('此模块[%s]未安装', $mid));
        }

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        foreach (['price', 'price_sku', 'price_quantity', 'price_sn'] as $fname) {
            if (!\Phpcmf\Service::M()->db->fieldExists($fname, $table)) {
                $this->_admin_msg(0, dr_lang('此模块[%s]没有创建[%s]字段，无法继续', $mid, $fname));
            }
        }

        $data = \Phpcmf\Service::M('app')->get_config(APP_DIR);

        if (IS_AJAX_POST) {
            $data['module'][$mid] = \Phpcmf\Service::L('input')->post('data');
            \Phpcmf\Service::M('app')->save_config(APP_DIR, $data);
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '内容模块权限' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-gears'],
                    '配置属性' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/edit{dir='.$mid.'}', 'fa fa-cog'],
                    'help' => [636],
                ]
            ),
            'data' => isset($data['module'][$mid]) ? $data['module'][$mid] : [],
            'ptype' => [
                0 => '付款→完成',
                1 => '付款→发货→完成',
                2 => '付款→收货→完成',
                3 => '付款→发货→收货→完成',
            ],
            'module' => $row,
        ]);
        \Phpcmf\Service::V()->display('module_config.html');
    }

    public function show() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_admin_msg(0, dr_lang('此模块[%s]未安装', $mid));
        }

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        foreach (['price', 'price_sku', 'price_quantity', 'price_sn'] as $fname) {
            if (!\Phpcmf\Service::M()->db->fieldExists($fname, $table)) {
                $this->_admin_msg(0, dr_lang('此模块[%s]没有创建[%s]字段，无法继续', $mid, $fname));
            }
        }

        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '内容模块权限' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-gears'],
                    '下单购买代码' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/show{dir='.$mid.'}', 'fa fa-cog'],
                    'help' => [636],
                ]
            ),
            'module' => $row,
            'show_code' => str_replace('{dirname}', $mid, file_get_contents(APPPATH.'Code/Show.html')),
            'cart_url_code' => str_replace('{dirname}', $mid, file_get_contents(APPPATH.'Code/Cart_url.html')),
            'list_cart_code' => str_replace('{dirname}', $mid, file_get_contents(APPPATH.'Code/Cart_list.html')),
        ]);
        \Phpcmf\Service::V()->display('module_code.html');
    }

    // 创建模块字段
    public function add() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_json(0, dr_lang('此模块[%s]未安装', $mid));
        }

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        foreach (['price', 'price_sku', 'price_quantity', 'price_sn'] as $fname) {
            if (\Phpcmf\Service::M()->db->fieldExists($fname, $table)) {
                $this->_json(0, dr_lang('此模块[%s]已经创建过[%s]字段，无法继续', $mid, $fname));
            }
        }

        if (\Phpcmf\Service::M()->db->table('field')->where('fieldname', 'price')->where('relatedid', $row['id'])->where('relatedname', 'module')->countAllResults()) {
            $this->_json(0, dr_lang('此模块[%s]在模块字段中已经创建过[%s]字段，无法继续', $mid, 'price'));
        }

        \Phpcmf\Service::M()->db->table('field')->insert(array(
            'name' => '价格',
            'ismain' => 1,
            'setting' => dr_array2string([
                'option' => [
                    'payfile' => 'order.html',
                    'is_finecms' => '1',
                    'field' => [
                        'price',
                        'quantity',
                        'sn',
                    ],
                ]
            ]),
            'issystem' => 1,
            'ismember' => 1,
            'disabled' => 0,
            'fieldname' => 'price',
            'fieldtype' => 'Pays',
            'relatedid' => $row['id'],
            'relatedname' => 'module',
            'displayorder' => 0,
        ));

        $sql = 'ALTER TABLE `'.$table.'` ADD `price` DECIMAL(9,2) NULL , ADD `price_sku` TEXT NULL , ADD `price_quantity` INT(10) UNSIGNED NULL , ADD `price_sn` VARCHAR(10) NULL';
        \Phpcmf\Service::M()->query($sql);


        \Phpcmf\Service::M('cache')->sync_cache('');
        $this->_json(1, dr_lang('安装成功'));
    }

    // 删除模块字段
    public function del() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_json(0, dr_lang('此模块[%s]未安装', $mid));
        }

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        foreach (['price', 'price_sku', 'price_quantity', 'price_sn'] as $fname) {
            if (\Phpcmf\Service::M()->db->fieldExists($fname, $table)) {
                \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` DROP `'.$fname.'`;');
            }
        }

        \Phpcmf\Service::M()->db->table('field')->where('fieldname', 'price')->where('relatedid', $row['id'])->where('relatedname', 'module')->delete();

        \Phpcmf\Service::M('cache')->sync_cache('');
        $this->_json(1, dr_lang('卸载成功'));
    }
}
